package com.chainyoung.component_tmperson.ui.mvp.presenter;

import com.chainyoung.common.appcommon.entity.HdWalletCache;
import com.chainyoung.common.base.CommonSubscriber;
import com.chainyoung.common.di.scope.ActivityScope;
import com.chainyoung.common.di.scope.FragmentScope;
import com.chainyoung.common.mvp.BasePresenter;
import com.chainyoung.common.utils.RxUtils;
import com.chainyoung.component_tmperson.entity.PurseAddr;
import com.chainyoung.component_tmperson.ui.mvp.contract.WalletAddrContract;
import com.chainyoung.component_tmperson.ui.mvp.contract.WalletLocalContract;

import java.util.List;
import java.util.TreeMap;

import javax.inject.Inject;

@FragmentScope
public class WalletLocalPresenter extends BasePresenter<WalletLocalContract.Model, WalletLocalContract.View> {


    @Inject
    public WalletLocalPresenter(WalletLocalContract.Model model, WalletLocalContract.View rootView) {
        super(model, rootView);
    }


    public void getSingleEthBalance(final String addr, final boolean isFirst) {
        addSubscribe(
                mModel.getSingleEthBalance(addr)
                        .compose(RxUtils.<String>rxSchedulerHelper())
                        .subscribeWith(new CommonSubscriber<String>(mView) {
                            @Override
                            protected void onStart() {
                                super.onStart();
                                if (isFirst) {
                                    mView.showLoading("加载数据中");
                                }
                            }

                            @Override
                            public void onComplete() {
                                if (isFirst) {
                                    mView.hideLoading();
                                }
                            }

                            @Override
                            public void onNext(String data) {
                                mView.onGetSingleEthBalanceSuccess(data,addr);
                            }
                        })
        );
    }


    public void getUsdCnyRate(String from,String to) {
        addSubscribe(
                mModel.getUsdCnyRate(from,to)
                        .compose(RxUtils.<String>rxSchedulerHelper())
                        .subscribeWith(new CommonSubscriber<String>(mView) {
                            @Override
                            protected void onStart() {
                                super.onStart();

                            }

                            @Override
                            public void onComplete() {
                                //mView.hideLoading();
                            }

                            @Override
                            public void onNext(String data) {
                                mView.onGetUsdCnyRateSuccessed(data);
                            }
                        })
        );
    }


    public void getEthPrice() {
        addSubscribe(
                mModel.getEthPrice()
                        .compose(RxUtils.<String>rxSchedulerHelper())
                        .subscribeWith(new CommonSubscriber<String>(mView) {
                            @Override
                            protected void onStart() {
                                super.onStart();

                            }

                            @Override
                            public void onComplete() {
                                //mView.hideLoading();
                            }

                            @Override
                            public void onNext(String data) {
                                mView.onGetEthPriceSuccessed(data);
                            }
                        })
        );
    }


    public void getAllHdWalletCaches(String coinType) {
        addSubscribe(mModel.getAllHdWalletCaches(coinType)
                .compose(RxUtils.<List<HdWalletCache>>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<List<HdWalletCache>>(mView) {
                    @Override
                    public void onNext(List<HdWalletCache> data) {
                        mView.onGetAllHdWalletCaches(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                }));


    }

    public void addHdWalletCache(HdWalletCache item) {
        addSubscribe(mModel.addHdWalletCache(item)
                .compose(RxUtils.<HdWalletCache>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<HdWalletCache>(mView,false) {
                    @Override
                    public void onNext(HdWalletCache data) {
                        mView.onAddHdWalletCacheSuccess(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                }));
    }






}

